<?php

declare(strict_types=1);

namespace app\index\controller;

use addons\OssBackup\facade\OssBackup;
use app\common\model\Logs;
use app\common\model\Statistics;
use app\common\model\Draw;
use addons\Target\facade\Target;

class Callback
{

    /**
     * @return void
     * 自动备份
     */
    public function autoOssBackup()
    {
        try {
            $route = 'callback/autoOssBackup.log';
            $result = OssBackup::auto();
            Logs::write($route, json_encode($result));
        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());
        }
    }

    /**
     * @return void
     * 更新统计
     */
    public function statistics()
    {
        Statistics::_timing();
    }
    /**
     * 定时
     */
    public function timing($type)
    {
        $route = 'callback/' . $type . '.log';
        try {
            switch ($type) {
                case 'groupOrderSettleAm':
                    $res = \app\common\model\WholesaleOrder::groupOrderSettleAm(); //上午缔造结算
                    break;
                case 'groupOrderSettlePm':
                    $res = \app\common\model\WholesaleOrder::groupOrderSettlePm(); //下午缔造结算
                    break;
                case 'groupOrderWin':
                    $res = \app\common\model\WholesaleOrder::groupOrderWin(); //绿色权益订单
                    break;
                case 'groupOrderFail':
                    $res = \app\common\model\WholesaleOrder::groupOrderFail(); //红色权益订单
                    break;
                case 'groupOrderTake':
                    $res = \app\common\model\WholesaleOrder::groupOrderTake(); //黄色权益订单
                    break;
                case 'isValid':
                    $res = \app\common\model\User::isValid(); //有效会员
                    break;
                case 'settle':
                    $res = \app\common\model\Performance::settle(); //业绩/加权结算
                    break;
                default:
                    return ['code' => -1, 'msg' => '参数错误'];
            }
            if ($res['code'] == 1 && $type != 'isValid') {
                Logs::write($route, json_encode($res));
            }
            if ($res['code'] == -1 && $type == 'isValid') {
                Logs::write($route, json_encode($res));
            }
        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());
        }
    }

    public function alipayCallback()
    {
        try {
            $route = 'callback/alipayCallback.log';
            $pay = \AliPay\App::instance(config('pay')['alipay']);
            $data = $pay->notify();
            if (in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
                // @todo 更新订单状态，支付完成

                Logs::write($route, '订单支付成功');
                return 'SUCCESS';
            } else {
                // @todo 回调验证失败处理
                Logs::write($route, '回调验证失败:' . json_encode($data));
            }
        } catch (\Exception $e) {
            // @todo 异常处理
            Logs::write($route, $e->getMessage());
        }
    }


    public function wechatCallback()
    {
        try {
            $route = 'callback/wechatCallback.log';
            $wechat = \WeChat\Pay::instance(config('pay')['wechat']);
            $data = $wechat->getNotify();
            if ($data['return_code'] === 'SUCCESS' && $data['result_code'] === 'SUCCESS') {
                // @todo 更新订单状态，支付完成


                // 返回接收成功的回复
                Logs::write($route, '订单支付成功');
                return $wechat->getNotifySuccessReply();
            } else {
                // @todo 回调验证失败处理
                Logs::write($route, '回调验证失败：' . json_encode($data));
            }
        } catch (\Exception $e) {
            // @todo 异常处理
            Logs::write($route, $e->getMessage());
        }
    }
    /**
     * 处理提现
     */
    public function draw()
    {
        try {
            $list =Draw::where([['status','=',2],['confirm_time','=',0]])->select();
            if ($list->isEmpty()) {
                return -2;
            }
            foreach ($list as $v) {
                Target::Account()->basics($v['uid'], 'help_money', $v['mutual'], '提现互助基金', '+')
                    ->createNormal();
                    $v->confirm_time = time();
                    $v->save();
            }
            return 1;
        }catch (\Exception $e){
            return -1;
        }
    }
}
